﻿package com.jfdesgagne.utils {
	import flash.xml.*;
/***************************************************
	Class XMLFileParser AS3
----------------------------------------------------
	This file parse XML file into Actionscript Object.
	If a node has more than 1 child with the same name, 
	an array is created with the children contents.
	
	The object created will have this structure:<br/>
	- obj {
		nodeName : {
			attributesName : Contain the value of the attribute
			nodeValue : Contain the "nodeValue" data
	}
----------------------------------------------------
	History of file
----------------------------------------------------
	2007-07-18 --> File created 
	2007-07-18 --> Method Added --> parseXML()
***************************************************/ 
public class XMLFileParser{
/*
	About: Description
	This file parse XML file into Actionscript Object.
	If a node has more than 1 child with the same name, 
	an array is created with the children contents.
	
	The object created will have this structure:
	>obj {
	>	nodeName : {
	>		attributesName : Contain the value of the attribute
	>		nodeValue : Contain the "nodeValue" data
	
	
	About: Author		
	Tommy Rochette tom[-at-]troisiemeoeil.ca
	
	About: License		
	Creative Commons http://creativecommons.org/licenses/by-nc-sa/2.5/legalcode
	
	About: File Created    
	July 18th 2007
	
	About: Last modifications	
	July 18th 2007
*/
/***************************************************
	Variables 
***************************************************/
	/* Variable: xfpResult 
	 Result of parsing*/
	private var xfpResult : Object;
	/* Variable: xfpXml 
	 Xml document to parse.*/
	private var xfpXml	  : XMLDocument;
/***************************************************
	Private method
***************************************************/
	/*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		Constructor: 	translateXml
						This function translate an XMLFile into an object.
						This function was modified from XML2Object.as written
						in AS2. At the time this function was written, the file
						was availaible there (http://www.unfitforprint.com/files/XML2Object.as).
						
		Parameters:		
						...arguments 	  - Argument collector.
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
	private function translateXml(...arguments:Array):void{
		var nodes , node;
		var old_path : String ;
		if (arguments[1] == undefined) {
			arguments[1] = this;
			arguments[2] = "xfpResult";
		}
		arguments[1] = arguments[1][arguments[2]];
		if (arguments[0] == undefined) {
			arguments[0] = this.xfpXml;
			arguments[0].ignoreWhite = true;
		}
		if (arguments[0].hasChildNodes()) {
			nodes = arguments[0].childNodes;
			if (arguments[3] != undefined) {
				old_path = arguments[1];
				arguments[1] = arguments[1][arguments[3]];
			}
			while (nodes.length > 0) {
				node = nodes.shift();
				if (node.nodeName != undefined) {
					var newObj = new Object ();
					for(var key:String in node.attributes){
						newObj[key] = node.attributes[key];
					}
					if(node.childNodes.length>0){
						newObj.nodeValue = node.firstChild.nodeValue;
					}else{
						newObj.nodeValue = "";
					}
					if (arguments[3] != undefined) {
						old_path = arguments[1];
					}
					if (arguments[1][node.nodeName] != undefined) {
						if (arguments[1][node.nodeName] is Array) {
							arguments[1][node.nodeName].push (newObj);
							arguments[2] = node.nodeName;
							arguments[3] = arguments[1][node.nodeName].length - 1;
						} else {
							var copyObj = arguments[1][node.nodeName];
							arguments[1][node.nodeName] = new Array ();
							arguments[1][node.nodeName].push (copyObj);
							arguments[1][node.nodeName].push (newObj);
							arguments[2] = node.nodeName;
							arguments[3] = arguments[1][node.nodeName].length - 1;
						}
					} else {
						arguments[1][node.nodeName] = newObj;
						arguments[2] = node.nodeName;
						arguments[3] = undefined;
					}
				}
				if (node.hasChildNodes ()) {
					this.translateXml (node, arguments[1], arguments[2], arguments[3]);
				}
			}
		}
	}
/***************************************************
	Public method
***************************************************/
	/*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		Constructor: 	parseXML
						Initialize data parsing.
						
		Parameters:		
						_xmlDoc - XML document to parse.
						_object - Object to return data.
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
	public function parseXML(_xmlDoc:XMLDocument,_object:Object):void{
		this.xfpXml     = _xmlDoc;
		this.xfpResult  = _object;
		this.translateXml();
		_object = this.xfpResult;
	}
}
}